package org.jeecg.modules.online.low.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.online.low.constant.LowAppMenuType;
import org.jeecg.modules.online.low.entity.LowAppMenu;
import org.jeecg.modules.online.low.service.ILowAppMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: low_app_menu
 * @Author: jeecg-boot
 * @Date: 2021-07-13
 * @Version: V1.0
 */
//@Api(tags = "low_app_menu")
@RestController("lowAppMenuController")
@RequestMapping("/online/lowAppMenu")
@Slf4j
public class LowAppMenuController extends JeecgController<LowAppMenu, ILowAppMenuService> {
    @Autowired
    private ILowAppMenuService lowAppMenuService;

    /**
     * 分页列表查询
     *
     * @param lowAppMenu
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "low_app_menu-分页列表查询")
    //@ApiOperation(value = "low_app_menu-分页列表查询", notes = "low_app_menu-分页列表查询")
    @GetMapping(value = "/list")
    public Result queryPageList(
            LowAppMenu lowAppMenu,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req
    ) {
        QueryWrapper<LowAppMenu> queryWrapper = QueryGenerator.initQueryWrapper(lowAppMenu, req.getParameterMap());
        Page<LowAppMenu> page = new Page<>(pageNo, pageSize);
        IPage<LowAppMenu> pageList = lowAppMenuService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 创建分组
     *
     * @param lowAppMenu
     * @return
     */
    @PostMapping(value = "/createGroup")
    public Result createGroup(@RequestBody LowAppMenu lowAppMenu) {
        lowAppMenu.setType(LowAppMenuType.GROUP);
        lowAppMenu.setDelFlag(CommonConstant.DEL_FLAG_0);
        lowAppMenuService.saveMenu(lowAppMenu);
        return Result.OK("创建成功！");
    }

    /**
     * 移动菜单到分组（parenId置空即可移出分组）
     *
     * @param lowAppMenu
     * @return
     */
    @PostMapping(value = "/moveToGroup")
    public Result moveToGroup(@RequestBody LowAppMenu lowAppMenu) {
        lowAppMenuService.moveToGroup(lowAppMenu);
        return Result.OK("移动成功！");
    }

    /**
     * 排序向上移，orderNum - 1
     *
     * @param lowAppMenu
     * @return
     */
    @PostMapping(value = "/orderToUp")
    public Result orderToUp(@RequestBody LowAppMenu lowAppMenu) {
        LowAppMenu menu = lowAppMenuService.getById(lowAppMenu.getId());
        if (menu == null) {
            return Result.error("实体不存在");
        }
        lowAppMenuService.orderToUp(menu);
        return Result.OK("向上移动成功");
    }

    /**
     * 排序向下移 orderNum + 1
     *
     * @param lowAppMenu
     * @return
     */
    @PostMapping(value = "/orderToDown")
    public Result orderToDown(@RequestBody LowAppMenu lowAppMenu) {
        LowAppMenu menu = lowAppMenuService.getById(lowAppMenu.getId());
        if (menu == null) {
            return Result.error("实体不存在");
        }
        lowAppMenuService.orderToDown(menu);
        return Result.OK("向下移动成功");
    }

    /**
     * 添加
     *
     * @param lowAppMenu
     * @return
     */
    @AutoLog(value = "low_app_menu-添加")
    //@ApiOperation(value = "low_app_menu-添加", notes = "low_app_menu-添加")
    @PostMapping(value = "/add")
    public Result add(@RequestBody LowAppMenu lowAppMenu) {
        lowAppMenu.setDelFlag(CommonConstant.DEL_FLAG_0);
        lowAppMenuService.saveMenu(lowAppMenu);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param lowAppMenu
     * @return
     */
    @AutoLog(value = "low_app_menu-编辑")
    //@ApiOperation(value = "low_app_menu-编辑", notes = "low_app_menu-编辑")
    @PutMapping(value = "/edit")
    public Result edit(@RequestBody LowAppMenu lowAppMenu) {
        lowAppMenuService.updateById(lowAppMenu);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "low_app_menu-通过id删除")
    //@ApiOperation(value = "low_app_menu-通过id删除", notes = "low_app_menu-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam(name = "id") String id) {
        lowAppMenuService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "low_app_menu-批量删除")
    //@ApiOperation(value = "low_app_menu-批量删除", notes = "low_app_menu-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result deleteBatch(@RequestParam(name = "ids") String ids) {
        this.lowAppMenuService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "low_app_menu-通过id查询")
    //@ApiOperation(value = "low_app_menu-通过id查询", notes = "low_app_menu-通过id查询")
    @GetMapping(value = "/queryById")
    public Result queryById(@RequestParam(name = "id") String id) {
        LowAppMenu lowAppMenu = lowAppMenuService.getById(id);
        if (lowAppMenu == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(lowAppMenu);
    }

    /**
     * 获取被逻辑删除的菜单，无分页
     */
    @GetMapping("/recycleBin")
    public Result getRecycleBin() {
        List<LowAppMenu> list = lowAppMenuService.queryLogicDeleted();
        return Result.OK(list);
    }

    /**
     * 还原被逻辑删除的菜单
     *
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "/revertRecycleBin", method = RequestMethod.PUT)
    public Result revertRecycleBin(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        String ids = jsonObject.getString("ids");
        if (org.apache.commons.lang.StringUtil.isNotBlank(ids)) {
            LowAppMenu updateUser = new LowAppMenu();
            updateUser.setUpdateBy(JwtUtil.getUserNameByToken(request));
            updateUser.setUpdateTime(new Date());
            lowAppMenuService.revertLogicDeleted(Arrays.asList(ids.split(",")), updateUser);
        }
        return Result.ok("还原成功");
    }

    /**
     * 彻底删除菜单
     *
     * @param ids 被删除的ID，多个id用半角逗号分割
     * @return
     */
    @RequestMapping(value = "/deleteRecycleBin", method = RequestMethod.DELETE)
    public Result deleteRecycleBin(@RequestParam("ids") String ids) {
        if (org.apache.commons.lang.StringUtil.isNotBlank(ids)) {
            lowAppMenuService.removeLogicDeleted(Arrays.asList(ids.split(",")));
        }
        return Result.ok("删除成功");
    }

    /**
     * 导出excel
     *
     * @param request
     * @param lowAppMenu
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LowAppMenu lowAppMenu) {
        return super.exportXls(request, lowAppMenu, LowAppMenu.class, "low_app_menu");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LowAppMenu.class);
    }

}
